<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户id">
          <el-input v-model="form.bd_id" placeholder="用户id" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="状态">
            <el-option label="生效" value="0" />
            <el-option label="失效" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="uid" label="用户uid" align="center" width="180" />
          <el-table-column prop="bd_id" label="bdId" align="center" width="180" />
          <el-table-column prop="remark" label="备注" align="center" width="180" />
          <el-table-column label="状态" align="center">
            <template #default="scope">
              <span>{{ parseInt(scope.row.status) === 0 ? '生效' : '失效' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="createtime"
            label="添加时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
          <el-table-column label="操作" align="center" width="180" fixed="right">
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
              >编辑</el-button>
              <el-button type="danger" @click="handleOperate(scope.$index, scope.row, 'delete')">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
    />
  </div>
</template>
  
  <script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime } from "@/utils/utils";
import {
  tradeList,
  tradeAdd,
  tradeUpdate,
  tradeDelete
} from "@/api/moby/live/live";
import DialogForm from "./components/dialogForm.vue";
const tableData = ref([]);
const loading = ref(false);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const dialogTitle = ref("工会添加");
const formData = {
  bd_id: "",
  status: ""
};
const form = reactive({ ...formData });
const dialogFormData = {
  uid: "",
  bd_id: "",
  remark: "",
  status: 0
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    bd_id: form.bd_id,
    status: form.status
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await tradeList(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
//新增
const onAdd = () => {
  dialogTitle.value = "工会添加";
  dialogVisible.value = true;
};
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    dialogTitle.value = "工会修改";
    Object.assign(dialogForm, {
      ...row
    });
    dialogVisible.value = true;
  } else if (type === "delete") {
    ElMessageBox.confirm("您是否确认删除?", "温馨提示", {
      type: "warning"
    }).then(async () => {
      const res = await tradeDelete({ id: row.id });

      ElMessage({
        message: res.message,
        type: "success"
      });

      getTbaleData();
    });
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;

  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;

  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async param => {
  try {
    btnLoading.value = true;
    let res;
    if (!param.id) {
      res = await tradeAdd(param);
    } else {
      res = await tradeUpdate(param);
    }

    ElMessage({
      message: res.message,
      type: "success"
    });

    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};

// 条数
const handleSizeChange = number => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = number => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
  